<template>
  <el-dialog :title="title" :visible="visible" @close="handleDialogClose" width="600px">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="right"
      label-width="100px"
      class="form"
    >
      <el-form-item label="部门名称:" prop="departmentName">
        <el-input class="form-item" v-model="form.departmentName" placeholder="请输入部门名称"></el-input>
      </el-form-item>
      <el-form-item label="部门描述:" prop="description">
        <el-input class="form-item" v-model="form.description" placeholder="请输入部门描述"></el-input>
      </el-form-item>
      <el-form-item label="上级部门:" prop="superiorId">
        <el-cascader
          v-model="form.superiorId"
          :options="departmentList"
          :props="props"
          clearable
          class="form-item"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import DepartmentService from '../../../../service/department'

export default {
  props: {
    title: {
      type: String,
      required: true,
      default: () => ''
    },
    show: {
      type: Boolean,
      required: true,
      default: () => false
    },
    data: {
      type: Object,
      required: false,
      default: () => {}
    }
  },
  data() {
    // 部门名称验证
    const checkDepartmentName = async (rule, value, callback) => {
      if (value.length === 0) {
        return callback(new Error('请输入部门名称'))
      } else {
        const result = await this.getIsExistsByDepartmentName(value)
        if (result.data) {
          return callback(new Error('该部门名称已被注册'))
        }
        callback()
      }
    }

    return {
      visible: false,
      departmentList: [],
      props: {
        value: 'departmentId',
        label: 'departmentName',
        checkStrictly: true,
        emitPath: false
      },
      form: {
        departmentId: '',
        departmentName: '',
        description: '',
        superiorId: ''
      },
      rules: {
        departmentName: {
          required: true,
          validator: checkDepartmentName,
          trigger: 'blur'
        },
        description: {
          required: true,
          message: '请输入部门描述',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    // 处理Dialog关闭事件
    handleDialogClose() {
      this.$emit('update:show')
      this.form = {
        departmentId: '',
        departmentName: '',
        description: '',
        superiorId: ''
      }
      this.$refs.form.resetFields()
    },
    // 判断部门名称是否已被注册
    getIsExistsByDepartmentName(keyword) {
      return DepartmentService.getIsExistsByDepartmentName(
        keyword,
        this.form.departmentId
      )
    },
    // 获取部门列表
    getDepartmentList() {
      DepartmentService.getDepartmentsByDepartmentName().then(res => {
        this.departmentList = this.recursionClearChildren(res.data)
      })
    },
    // 递归清除为空的children
    recursionClearChildren(list) {
      let arr = []
      list.forEach(item => {
        if (!item.children || !item.children.length) {
          delete item.children
        } else {
          item.children = this.recursionClearChildren(item.children)
        }
        arr.push(item)
      })
      return arr
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.departmentId) {
            // 清空 children 属性
            delete this.form.children

            DepartmentService.updateDepartment(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.$emit('success')
            })
          } else {
            DepartmentService.insertDepartment(this.form).then(res => {
              // 提示
              this.$message.success(res.message)
              // 关闭Dialog
              this.visible = false
              // 派发事件刷新Table
              this.getDepartmentList()
              this.$emit('success')
            })
          }
        }
      })
    }
  },
  mounted() {
    this.getDepartmentList()
  },
  watch: {
    show(value) {
      this.visible = value
    },
    data(value) {
      this.form = value || {
        departmentId: '',
        departmentName: '',
        description: '',
        superiorId: ''
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.form
  margin 0 30px
  .form-item
    width 300px
</style>
